// pages/Index/shopActive/invitation/invitation_detail/invitation_detail.js
const controller = require('../../../../controllers/controllerIndex').controller

Page({

  data: {
    invitationId:'',
    detail:{},
    imgList:[],
    receivePage:[],
    lastPage:false,
    totalRow:'',
    pageNumber:1,
    mobile:'',
    timer:''
  },

  onLoad: function (options) {
    this.setData({
      invitationId:options.invitationId
    })
    this.getInvitationDetail()
    this.getReceivePage(true)
  },

  onShow: function () {

  },
  onReachBottom(){
    if(this.data.lastPage)return
    this.getReceivePage()
  },
  // 获取邀请详情
  getInvitationDetail(){
    let value={
      invitationId:this.data.invitationId,
      sessionId:wx.getStorageSync('token'),
    }
    controller.invitationDetail(value).then(res=>{
      let imgList= res.data.image
      imgList=imgList?imgList.split(','):[]
      this.setData({
        detail:res.data,
        imgList
      })
    })
  },

  // 获取手机号搜索
  inputSearch(e){
    this.setData({
      mobile:e.detail.value
    })
    if(this.data.timer)clearTimeout(this.data.timer)
    this.data.timer = setTimeout(()=>{
      this.getReceivePage(true)
    },500)
  },
  // 获取领取列表
  getReceivePage(isFirst = false){
    if(isFirst){
      this.setData({
        pageNumber:1,
        receivePage:[]
      })
    }
    let pageNumber=this.data.pageNumber
    let receivePage=this.data.receivePage
    let value={
      shopId:wx.getStorageSync('shopId'),
      sessionId:wx.getStorageSync('token'),
      invitationId:this.data.invitationId,
      pageNumber,
      pageSize:20,
      mobile:this.data.mobile
    }
    pageNumber++
    controller.receivePage(value).then(res=>{
      receivePage=receivePage.concat(res.data.list)
      this.setData({
        lastPage:res.data.lastPage,
        totalRow:res.data.totalRow,
        receivePage,
        pageNumber
      })
    })
  },
  previewImg(e){
    const index = e.target.dataset.index
    const imgList = this.data.imgList
    wx.previewImage({
      current:imgList[index],
      urls: imgList,
    })
  },
  // 提前结束
  preEnd(){
    let value={
      invitationId:this.data.invitationId,
      shopId:wx.getStorageSync('shopId'),
      sessionId:wx.getStorageSync('token'),
    }
    controller.invitationSetOver(value).then(res=>{
      wx.navigateBack()
    })
  },

  onShareAppMessage: function () {

  }
})